package com.wash.car.biz.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wash.car.biz.VipServer;
import com.wash.car.dao.domain.AbBaseDomain;
import com.wash.car.dao.domain.CarVipDetailDomain;
import com.wash.car.dao.domain.CarVipDomain;
import com.wash.car.dao.mapper.CarVipDetailMapper;
import com.wash.car.dao.mapper.CarVipMapper;
import com.wash.car.dao.model.VipAddModel;
import com.wash.car.dao.model.VipPageModel;
import com.wash.car.dao.model.VipPageWhereModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class VipServerImpl implements VipServer {
    @Resource
    private CarVipMapper carVipMapper;

    @Resource
    private CarVipDetailMapper carVipDetailMapper;

    @Override
    @Transactional
    public void add(VipAddModel model) {
        CarVipDomain dbCarVipDomain = carVipMapper.selectOne(new CarVipDomain().setPlate(model.getPlate()));
        Integer type = 1;
        if (ObjectUtil.isEmpty(dbCarVipDomain)) {
            // add
            CarVipDomain carVipDomain = AbBaseDomain.setDefaultValue(CarVipDomain.class);
            carVipDomain.setPlate(model.getPlate()).setPhone(model.getPhone()).setEndTime(model.getEndTime());
            carVipDomain.setCreateUser(String.valueOf(model.getUserId())).setUpdateUser(String.valueOf(model.getUserId()));
            carVipMapper.insertSelective(carVipDomain);
        } else {
            //update
            CarVipDomain carVipDomain = AbBaseDomain.setDefaultValue(CarVipDomain.class);
            carVipDomain.setPlate(model.getPlate()).setPhone(model.getPhone()).setEndTime(model.getEndTime());
            carVipDomain.setCreateUser(String.valueOf(model.getUserId())).setUpdateUser(String.valueOf(model.getUserId()));
            carVipDomain.setId(dbCarVipDomain.getId());
            carVipMapper.updateByPrimaryKeySelective(carVipDomain);
            type = 2;
        }
        CarVipDetailDomain carVipDetailDomain = AbBaseDomain.setDefaultValue(CarVipDetailDomain.class);
        carVipDetailDomain.setType(type).setPlate(model.getPlate()).setEndTime(model.getEndTime());
        carVipDetailDomain.setCreateUser(String.valueOf(model.getUserId())).setUpdateUser(String.valueOf(model.getUserId()));
        carVipDetailMapper.insertSelective(carVipDetailDomain);
    }

    @Override
    public PageInfo<VipPageModel> page(VipPageWhereModel model) {
        PageHelper.startPage(model.getPageNum(), model.getPageSize(), model.getOrderBySql());
        List<VipPageModel> list = carVipMapper.selectPage(BeanUtil.beanToMap(model));
        PageInfo<VipPageModel> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }


}
